package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfPaymentDAO;
import com.ljkj.common.db.domain.ZjfPayment;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfPaymentService {
	
	private ZjfPaymentDAO zjfPaymentDAO;
	private TLogService tLogService;
	private ZjfUserService zjfUserService;


	/**
	 * 获取产品
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getPaymentsByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfPayment.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfPaymentDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfPaymentDAO.setPageMaxCount(pageSize);
		List<ZjfPayment> zjfPayments = this.zjfPaymentDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfPayment zjfPayment : zjfPayments){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("paymentId",zjfPayment.getPaymentId());
			entry_map.put("userId",zjfPayment.getUserId());
			entry_map.put("orderId",zjfPayment.getOrderId());
			entry_map.put("userName",zjfPayment.getUserName());
			entry_map.put("paymentMoney",zjfPayment.getPaymentMoney());
			entry_map.put("paymentType",zjfPayment.getPaymentType());
			entry_map.put("paymentStatus",zjfPayment.getPaymentStatus());
			entry_map.put("paymentTradeNo",zjfPayment.getPaymentTradeNo());
			entry_map.put("paymentOutNo",zjfPayment.getPaymentOutNo());
			entry_map.put("paymentTradeNoReturn",zjfPayment.getPaymentTradeNoReturn());
			entry_map.put("paymentOutNoReturn",zjfPayment.getPaymentOutNoReturn());
			entry_map.put("paymentResult",zjfPayment.getPaymentResult());
			entry_map.put("paymentMemo",zjfPayment.getPaymentMemo());
			entry_map.put("paymentMessage",zjfPayment.getPaymentMessage());
			
			entry_map.put("createTime",zjfPayment.getCreateTime());
			entry_map.put("createUser", zjfPayment.getCreateUser());
			entry_map.put("updateTime", zjfPayment.getUpdateTime());
			entry_map.put("updateUser", zjfPayment.getUpdateUser());
			entry_map.put("disabledState", zjfPayment.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	/**
	 * 新增或更新退款记录
	 * @param 
	 * @return
	 */
	public String addOrEditPaymentInfo(ZjfPayment zjfPayment,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfPayment.getPaymentId() == null){
			
			tlog.setOpType(1);
			tlog.setContent("新增退款记录："+ zjfPayment.getOrderId());
			zjfPayment.setPaymentId(RandomCode.makeCode());
			zjfPayment.setCreateTime(DateTime.getDateTime());
			zjfPayment.setCreateUser(tuser.getLoginName());
			if(this.saveNewPayment(zjfPayment)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfPayment.getDisabledState()!=null && zjfPayment.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("退款记录状态："+zjfPayment.getOrderId()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改退款记录："+zjfPayment.getOrderId());
				}
				
				zjfPayment.setUpdateTime(DateTime.getDateTime());
				zjfPayment.setUpdateUser(tuser.getLoginName());
				this.zjfPaymentDAO.update(zjfPayment);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewPayment(ZjfPayment zjfPayment){
		try{
			this.zjfPaymentDAO.save(zjfPayment);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfPayment getPaymentById(String paymentId){
		return this.zjfPaymentDAO.findById(paymentId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfPaymentDAO(ZjfPaymentDAO zjfPaymentDAO) {
		this.zjfPaymentDAO = zjfPaymentDAO;
	}

	public void setZjfUserService(ZjfUserService zjfUserService) {
		this.zjfUserService = zjfUserService;
	}
}
